/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.session.providers;

import java.util.List;

import net.zarubsys.robotplayground.core.device.IDevice;
import net.zarubsys.robotplayground.core.device.NullDevice;
import net.zarubsys.robotplayground.core.modules.EmptyModuleConfiguration;
import net.zarubsys.robotplayground.core.modules.IModule;
import net.zarubsys.robotplayground.core.modules.IModuleConfiguration;
import net.zarubsys.robotplayground.core.session.DeviceSessionContainer;
import net.zarubsys.robotplayground.core.session.ModuleSessionContainer;
import net.zarubsys.robotplayground.core.session.Session;
import net.zarubsys.robotplayground.core.session.SessionsHelper;

/**
 * AbstractSimpleModuleGUIProvider
 * 
 * Subclass this class if you want to have simple GUI provider for your
 * Module. Override any method to alter the behavior.
 * 
 * In default the Module is expected not to be configurable with validation
 * based only on the presence of required devices and parameterized with the
 * {@link EmptyModuleConfiguration} config with no need for special action when
 * added or removed from the session.
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public abstract class AbstractSimpleModuleGUIProvider implements ISystemObjectGUIProvider {

	public AbstractSimpleModuleGUIProvider() {
		super();
	}

	@Override
	public SystemObjectGUIProviderClass getProviderClass() {
		return SystemObjectGUIProviderClass.MODULE;
	}

	@Override
	public int addObjectToSession(Session session, String id) {
		session.getModules().add(new ModuleSessionContainer(id, getConfiguration()));
		return session.getModules().size() - 1;
	}

	protected EmptyModuleConfiguration getConfiguration() {
		return EmptyModuleConfiguration.getInstance();
	}

	@Override
	public boolean reconfigure(Session session, int index) {
		return false;
	}

	@Override
	public boolean isConfigurable(String id) {
		return false;
	}

	@Override
	public boolean remove(Session session, int index) {
		session.getModules().remove(index);
		return true;
	}
	
	@Override
	public String getDescription(Session session, int index) {
		return getDescription(session.getModules().get(index).getModuleId());
	}
	
	@Override
	public String validate(Session session, int index) {
		String moduleId = session.getModules().get(index).getModuleId();
		IModule<IDevice<?>, IModuleConfiguration> module = SessionsHelper.getInstance().getModule(moduleId);
		String requiredDevice = module.getRequiredDevice();
		if (NullDevice.ID.equals(requiredDevice)) return null;
		List<DeviceSessionContainer> devices = session.getDevices();
		for (DeviceSessionContainer deviceCont : devices) {
			if (deviceCont.getDeviceId().equals(requiredDevice)) return null;
		}
		ISystemObjectGUIProvider missingDeviceProvider = SystemObjectGUIProvidersHelper.getInstance().getProvider(SystemObjectGUIProviderClass.DEVICE, requiredDevice);
		if (missingDeviceProvider == null) return "Missing required device for id '" + requiredDevice + "'. There is even no provider for it.";
		return "Missing required device '" + missingDeviceProvider.getDescription(requiredDevice) + "'.";
	}

}
